//
//  NewsViewController.swift
//  Yun
//
//  Created by 新宝 on 2017/6/8.
//  Copyright © 2017年 新宝. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import HandyJSON
import SnapKit
import DGElasticPullToRefresh

protocol NewsViewDelegate: class{
    func newsView(_ controller:NewsViewController,currentIndex:Int)
    
}

class NewsViewController: UIViewController {
    weak var newsViewDelegate:NewsViewDelegate?
    
    var sportModelArr:NSMutableArray=NSMutableArray()
    var headlineModelArr:NSMutableArray=NSMutableArray()
    var economicsModelArr:NSMutableArray=NSMutableArray()
    var recreationModelArr:NSMutableArray=NSMutableArray()
    var militaryModelArr:NSMutableArray=NSMutableArray()
    var historyModelArr:NSMutableArray=NSMutableArray()
    var technologyModelArr:NSMutableArray=NSMutableArray()
    var startOffset:CGFloat=0
   
    
    //请求数据
    func loadingData()
    {
        NetworkTool.loadSportsData(controller: "体育") { (response) in
            self.sportModelArr=response as! NSMutableArray
            self.newsTableView.reloadData()
            self.loadingView.removeFromSuperview()
        }
        
    }
    //下载数据提示View
    lazy var loadingView:LoadingView={
        let loadingView=LoadingView(frame:CGRect(x:0,y:0,width:screenWidth,height:screenHeight))
        return loadingView
    }()
    
    
    
    //文字滑动栏
    lazy var headTitleView:HeadTitleView={
         let headTitleView=HeadTitleView()
        headTitleView.headTitleDelegate=self
         return headTitleView
    }()
    
    lazy var newsScollView:UIScrollView={
        let newsScollView=UIScrollView()
        newsScollView.frame=CGRect(x: 0, y: 64, width: self.view.frame.size.width, height: self.view.frame.size.height)
        newsScollView.backgroundColor=UIColor.yellow
        newsScollView.delegate=self as UIScrollViewDelegate
        newsScollView.contentSize=CGSize(width:self.view.frame.size.width*7,height:self.view.frame.size.height)
        newsScollView.isPagingEnabled=true
        newsScollView.bounces=false
        newsScollView.indicatorStyle = .white
        return newsScollView
        
    }()
    
    lazy var newsTableView:UITableView={
        let newsTableView=UITableView()
        newsTableView.rowHeight=100;
        newsTableView.frame=CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        newsTableView.register(UINib(nibName:"NewsTableViewCell", bundle:nil), forCellReuseIdentifier: "newsCell")
        newsTableView.delegate=self as UITableViewDelegate;
        newsTableView.dataSource=self as UITableViewDataSource;
        newsTableView.separatorStyle = .none
        NetworkTool.loadAllData(controllerTitle: "体育", completionHandler: { (response) in
            self.sportModelArr=response as! NSMutableArray
            self.newsTableView.reloadData()
            newsTableView.separatorStyle = .singleLine
            self.loadingView.removeFromSuperview()
            
            
        })
    
        return newsTableView
        
    }()

    lazy var headlinesTableView:UITableView={
        let headlinesTableView=UITableView()
        headlinesTableView.rowHeight=100
        headlinesTableView.frame=CGRect(x: self.view.frame.size.width, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        headlinesTableView.register(UINib(nibName:"HeadlineTableViewCell", bundle:nil), forCellReuseIdentifier: "headlineCell")
        headlinesTableView.delegate=self as UITableViewDelegate
        headlinesTableView.dataSource=self as UITableViewDataSource
        headlinesTableView.separatorStyle = .singleLine
//        NetworkTool.loadAllData(controllerTitle: "头条", completionHandler: { (response) in
//            self.headlineModelArr=response as! NSMutableArray
//            self.headlinesTableView.reloadData()
//            headlinesTableView.separatorStyle = .singleLine
//           // self.loadingView.removeFromSuperview()
//        })
        
        return headlinesTableView
    }()
    
    lazy var economicTableView:UITableView={
        let economicTableView=UITableView()
        economicTableView.rowHeight=100;
        economicTableView.frame=CGRect(x: self.view.frame.size.width*2, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        economicTableView.register(UINib(nibName:"EconomicTableViewCell", bundle:nil), forCellReuseIdentifier: "economicCell")
        economicTableView.delegate=self as UITableViewDelegate;
        economicTableView.dataSource=self as UITableViewDataSource;
//        NetworkTool.loadAllData(controllerTitle: "财经", completionHandler: { (response) in
//            self.economicsModelArr=response as! NSMutableArray
//            self.economicTableView.reloadData()
//            //self.loadingView.removeFromSuperview()
//        })
        
       return economicTableView
    }()
    
    lazy var recreationTableView:UITableView={
        let recreationTableView=UITableView()
        recreationTableView.rowHeight=100;
        recreationTableView.frame=CGRect(x: self.view.frame.size.width*3, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        recreationTableView.register(UINib(nibName:"RecreationTableViewCell", bundle:nil), forCellReuseIdentifier: "recreationCell")
        recreationTableView.delegate=self as UITableViewDelegate;
        recreationTableView.dataSource=self as UITableViewDataSource;
//        NetworkTool.loadAllData(controllerTitle: "娱乐", completionHandler: { (response) in
//            self.recreationModelArr=response as! NSMutableArray
//            self.recreationTableView.reloadData()
//            //self.loadingView.removeFromSuperview()
//        })

        return recreationTableView
    }()
    
    lazy var militaryTableView:UITableView={
        let militaryTableView=UITableView()
        militaryTableView.rowHeight=100;
        militaryTableView.frame=CGRect(x: self.view.frame.size.width*4, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        militaryTableView.register(UINib(nibName:"MilitaryTableViewCell", bundle:nil), forCellReuseIdentifier: "militaryCell")
        militaryTableView.delegate=self as UITableViewDelegate;
        militaryTableView.dataSource=self as UITableViewDataSource;
//        NetworkTool.loadAllData(controllerTitle: "军事", completionHandler: { (response) in
//            self.militaryModelArr=response as! NSMutableArray
//            self.militaryTableView.reloadData()
//           // self.loadingView.removeFromSuperview()
//        })

               return militaryTableView
    }()
    
    lazy var historyTableView:UITableView={
        let historyTableView=UITableView()
        historyTableView.rowHeight=100;
        historyTableView.frame=CGRect(x: self.view.frame.size.width*5, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        historyTableView.register(UINib(nibName:"HistoryTableViewCell", bundle:nil), forCellReuseIdentifier: "historyCell")
        historyTableView.delegate=self as UITableViewDelegate;
        historyTableView.dataSource=self as UITableViewDataSource;
//        NetworkTool.loadAllData(controllerTitle: "历史", completionHandler: { (response) in
//            self.historyModelArr=response as! NSMutableArray
//            self.historyTableView.reloadData()
//            //self.loadingView.removeFromSuperview()
//        })

        return historyTableView
    }()
    
    lazy var technologyTableView:UITableView={
        let technologyTableView=UITableView()
        technologyTableView.rowHeight=100;
        technologyTableView.frame=CGRect(x: self.view.frame.size.width*6, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        technologyTableView.register(UINib(nibName:"TechnologyTableViewCell", bundle:nil), forCellReuseIdentifier: "technologyCell")
        technologyTableView.delegate=self as UITableViewDelegate;
        technologyTableView.dataSource=self as UITableViewDataSource;
//        NetworkTool.loadAllData(controllerTitle: "科技", completionHandler: { (response) in
//            self.technologyModelArr=response as! NSMutableArray
//            self.technologyTableView.reloadData()
//            //self.loadingView.removeFromSuperview()
//           
//        })
         return technologyTableView
    }()
    
    
    
    override func loadView() {
        super.loadView()
        //创建UI
        setupUI()
        
        //下载数据
       //self.loadingData()
        
    }
    
    fileprivate func setLoadingView()->DGElasticPullToRefreshLoadingViewCircle{
        let loadingView=DGElasticPullToRefreshLoadingViewCircle()
        loadingView.tintColor=UIColor.red
        return loadingView
    }

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        newsTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
            NetworkTool.loadSportsData(controller: "体育") { (response) in
                self?.sportModelArr=response as! NSMutableArray
                self?.newsTableView.reloadData()
                self?.newsTableView.dg_stopLoading()
            }

            })
        }, loadingView: self.setLoadingView())
        newsTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))
        
        headlinesTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
            NetworkTool.loadHeadlinesData(controller: "头条") { (response) in
                self?.headlineModelArr=response as! NSMutableArray
                self?.headlinesTableView.reloadData()
                 self?.headlinesTableView.dg_stopLoading()
            }
            
           })
            }, loadingView: self.setLoadingView())
        headlinesTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))
        
       economicTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
        NetworkTool.loadEconomicData(controller: "财经") { (response) in
            self?.economicsModelArr=response as! NSMutableArray
            self?.economicTableView.reloadData()
            self?.economicTableView.dg_stopLoading()
        }
        
        })
            }, loadingView: self.setLoadingView())
       economicTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))
        
        recreationTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
            NetworkTool.loadRecreationData(controller: "娱乐") { (response) in
                self?.recreationModelArr=response as! NSMutableArray
                self?.recreationTableView.reloadData()
                self?.recreationTableView.dg_stopLoading()
            }
            
        })
            }, loadingView: self.setLoadingView())
        recreationTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))
        
        militaryTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
            NetworkTool.loadMilitaryData(controller: "军事") { (response) in
                self?.militaryModelArr=response as! NSMutableArray
                self?.militaryTableView.reloadData()
                self?.militaryTableView.dg_stopLoading()
            }
            
        })
            }, loadingView: self.setLoadingView())
        militaryTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))

        historyTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
            NetworkTool.loadHistoryData(controller: "历史") { (response) in
                self?.historyModelArr=response as! NSMutableArray
                self?.historyTableView.reloadData()
                self?.historyTableView.dg_stopLoading()
            }
            
        })
            }, loadingView: self.setLoadingView())
        historyTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))
        
        technologyTableView.dg_addPullToRefreshWithActionHandler({ [weak self]()->Void in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+Double(Int64(1.5*Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC), execute: {
            NetworkTool.loadTechnologyData(controller: "科技") { (response) in
                self?.technologyModelArr=response as! NSMutableArray
                self?.technologyTableView.reloadData()
                self?.technologyTableView.dg_stopLoading()
            }
            
        })
            }, loadingView: self.setLoadingView())
        technologyTableView.dg_setPullToRefreshFillColor(UIColor(red:31/255.0,green:155/255.0,blue:243/255.0,alpha:1.0))

        
    }
    
    
    deinit{
        
        newsTableView.dg_removePullToRefresh()
        headlinesTableView.dg_removePullToRefresh()
        economicTableView.dg_removePullToRefresh()
        recreationTableView.dg_removePullToRefresh()
        militaryTableView.dg_removePullToRefresh()
        historyTableView.dg_removePullToRefresh()
        technologyTableView.dg_removePullToRefresh()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}


//类扩展部分------------------------------------------------------------------
extension NewsViewController{
    fileprivate func setupUI(){
        //文字标题滑动View
        self.view.addSubview(headTitleView)
        self.view.addSubview(newsScollView)
        newsScollView.addSubview(headlinesTableView)
        newsScollView.addSubview(newsTableView)
        newsScollView.addSubview(economicTableView)
        newsScollView.addSubview(recreationTableView)
        newsScollView.addSubview(militaryTableView)
        newsScollView.addSubview(historyTableView)
        newsScollView.addSubview(technologyTableView)
        //添加下载数据提示View
        newsTableView.addSubview(loadingView)
        headTitleView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(0)
            make.left.equalTo(self.view).offset(0)
            make.right.equalTo(self.view).offset(0)
            make.height.equalTo(64);
        }
        //设置代理，newsViewDelegate指向类HeadTitleView的对象
         newsViewDelegate=headTitleView as NewsViewDelegate
    }
}


extension NewsViewController:UITableViewDelegate
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if(tableView==headlinesTableView)
        {
            var headlinesModel=HeadlinesModel()
            headlinesModel=headlineModelArr[indexPath.row] as! HeadlinesModel
            let webVC=WebViewController()
            webVC.urlString=headlinesModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)

        }
        else if(tableView==newsTableView)
        {
           var sportModel=SportModel()
            sportModel=sportModelArr[indexPath.row] as! SportModel
            let webVC=WebViewController()
            webVC.urlString=sportModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)
        }
        else if(tableView==economicTableView)
        {
            var economicModel=EconomicsModel()
            economicModel=economicsModelArr[indexPath.row] as! EconomicsModel
            let webVC=WebViewController()
            webVC.urlString=economicModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)
        }
       else if(tableView==recreationTableView)
        {
            var recreationModel=RecreationModel()
            recreationModel=recreationModelArr[indexPath.row] as! RecreationModel
            let webVC=WebViewController()
            webVC.urlString=recreationModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)
        }
        else if(tableView==militaryTableView)
        {
            var militaryModel=MilitaryModel()
            militaryModel=militaryModelArr[indexPath.row]as! MilitaryModel
            let webVC=WebViewController()
            webVC.urlString=militaryModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)
        }
        else if(tableView==historyTableView)
        {
            var historyModel=HistoryModel()
            historyModel=historyModelArr[indexPath.row]as! HistoryModel
            let webVC=WebViewController()
            webVC.urlString=historyModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)
        }
        else if(tableView==technologyTableView)
        {
            var technologyModel=TechnologyModel()
            technologyModel=technologyModelArr[indexPath.row]as! TechnologyModel
            let webVC=WebViewController()
            webVC.urlString=technologyModel.commentsUrl
            self.present(webVC, animated: true, completion: nil)
        }
}
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
}
 extension NewsViewController:UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if(tableView==newsTableView)
        {
            return sportModelArr.count
        }
        else if(tableView==headlinesTableView)
        {
            return headlineModelArr.count
        }
        else if(tableView==economicTableView)
        {
            return economicsModelArr.count
        }
        else if(tableView==recreationTableView)
        {
            return recreationModelArr.count
        }
        else if(tableView==militaryTableView)
        {
            return militaryModelArr.count
        }
        else if(tableView==historyTableView)
        {
            return historyModelArr.count
        }
        else if(tableView==technologyTableView)
        {
            return technologyModelArr.count
        }
        else{
            return 0
        }

        
}
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        if(tableView==headlinesTableView)
        {
            let cell=headlinesTableView.dequeueReusableCell(withIdentifier: "headlineCell", for: indexPath)as!HeadlineTableViewCell
            var headlinesModel=HeadlinesModel()
            headlinesModel=headlineModelArr[indexPath.row] as! HeadlinesModel
            let url=NSURL(string:(headlinesModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : headlinesModel.thumbnail!)
            let data=NSData(contentsOf:url! as URL)
            let image=UIImage(data: data! as Data)
            
            cell.iconImageView.image=image
            cell.titelLabel.text=headlinesModel.title
            cell.timeLabel.text=headlinesModel.updateTime
            headlinesTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
            cell.selectionStyle = .none
            return cell
        }
        else if(tableView==economicTableView)
        {
            let cell=economicTableView.dequeueReusableCell(withIdentifier: "economicCell", for: indexPath)as!EconomicTableViewCell
            var economicModel=EconomicsModel()
            economicModel=economicsModelArr[indexPath.row] as! EconomicsModel
            let url=NSURL(string:(economicModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : economicModel.thumbnail!)
            let data=NSData(contentsOf:url! as URL)
            let image=UIImage(data: data! as Data)
            
            cell.iconImageView.image=image
            cell.titleLabel.text=economicModel.title
            cell.timeLabel.text=economicModel.updateTime
            economicTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
            cell.selectionStyle = .none
            return cell
        }
        else if(tableView==recreationTableView)
        {
            let cell=recreationTableView.dequeueReusableCell(withIdentifier: "recreationCell", for: indexPath)as!RecreationTableViewCell
            var recreationModel=RecreationModel()
            recreationModel=recreationModelArr[indexPath.row] as! RecreationModel
            let url=NSURL(string:(recreationModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : recreationModel.thumbnail!)
            let data=NSData(contentsOf:url! as URL)
            let image=UIImage(data: data! as Data)
            
            cell.iconImageView.image=image
            cell.titleLabel.text=recreationModel.title
            cell.timeLabel.text=recreationModel.updateTime
            recreationTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
            cell.selectionStyle = .none
            return cell

        }
        else if(tableView==militaryTableView)
        {
            let cell=militaryTableView.dequeueReusableCell(withIdentifier: "militaryCell", for: indexPath)as!MilitaryTableViewCell
            var militaryModel=MilitaryModel()
            militaryModel=militaryModelArr[indexPath.row] as! MilitaryModel
            let url=NSURL(string:(militaryModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : militaryModel.thumbnail!)
            let data=NSData(contentsOf:url! as URL)
            let image=UIImage(data: data! as Data)
            
            cell.iconImageView.image=image
            cell.titleLabel.text=militaryModel.title
            cell.timeLabel.text=militaryModel.updateTime
            militaryTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
            cell.selectionStyle = .none
            return cell

            
        }
        else if(tableView==historyTableView)
        {
            let cell=historyTableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath)as!HistoryTableViewCell
            var historyModel=HistoryModel()
            historyModel=historyModelArr[indexPath.row] as! HistoryModel
            let url=NSURL(string:(historyModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : historyModel.thumbnail!)
            let data=NSData(contentsOf:url! as URL)
            let image=UIImage(data: data! as Data)
            
            cell.iconImageView.image=image
            cell.titleLabel.text=historyModel.title
            cell.timeLabel.text=historyModel.updateTime
            historyTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
            cell.selectionStyle = .none
            return cell

            
        }
        else if(tableView==technologyTableView)
        {
            let cell=technologyTableView.dequeueReusableCell(withIdentifier: "technologyCell", for: indexPath)as!TechnologyTableViewCell
            var technologyModel=TechnologyModel()
            technologyModel=technologyModelArr[indexPath.row] as! TechnologyModel
            let url=NSURL(string:(technologyModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : technologyModel.thumbnail!)
            let data=NSData(contentsOf:url! as URL)
           let image=UIImage(data: data! as Data)
            
            cell.iconImageView.image=image
            cell.titleLabel.text=technologyModel.title
            cell.timeLabel.text=technologyModel.updateTime
            technologyTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
            cell.selectionStyle = .none
            
            return cell

            
            
        }

    
        let cell=newsTableView.dequeueReusableCell(withIdentifier: "newsCell", for: indexPath)as!NewsTableViewCell
        var sportModel=SportModel()
        sportModel=sportModelArr[indexPath.row] as! SportModel
        let url=NSURL(string:(sportModel.thumbnail?.isEmpty)! ? "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg" : sportModel.thumbnail!)
        let data=NSData(contentsOf:url! as URL)
        let image=UIImage(data: data! as Data)
        cell.iconImageView.image=image
        cell.titelLabel.text=sportModel.title
        cell.timeLabel.text=sportModel.updateTime
        newsTableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
        cell.selectionStyle = .none
        return cell
     }
    
}

extension NewsViewController:HeadTitleViewDelegate{
    func titleView(_ titleView: HeadTitleView, index: Int) {
        //点击头部文字加载数据
        self.loadData(currentIndex: index)
        self.newsScollView.setContentOffset(CGPoint(x:Int(screenWidth)*index,y:0), animated: false)
        
    }
    
}

extension NewsViewController:UIScrollViewDelegate{

    //减速结束
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        contentEndScroll()
        scrollView.isScrollEnabled = true
    }
    //在用户结束拖动后被调用，decelerate 为 YES 时，结束拖动后会有减速过程。注，在 didEndDragging 之后，如果有减速过程，scroll view 的 dragging 并不会立即置为 NO，而是要等到减速结束之后，所以这个 dragging 属性的实际语义更接近 scrolling。
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            contentEndScroll()
        } else {
            //scrollView.isScrollEnabled = false
        }
    }
    
    private func contentEndScroll() {
        // 获取滚动到的位置
        let currentIndex = Int(newsScollView.contentOffset.x / screenWidth)
        //滑动ScollView下载当前页面数据
        self.loadData(currentIndex: currentIndex)
        self.newsViewDelegate?.newsView(self, currentIndex: currentIndex)

    }
    
    
    //加载数据
    func loadData(currentIndex:Int){
        if(currentIndex==0){
            NetworkTool.loadAllData(controllerTitle: "体育", completionHandler: { (response) in
                self.sportModelArr=response as! NSMutableArray
                self.newsTableView.reloadData()
                self.loadingView.removeFromSuperview()
            })

            
        }else if(currentIndex==1){
            NetworkTool.loadAllData(controllerTitle: "头条", completionHandler: { (response) in
                self.headlineModelArr=response as! NSMutableArray
                self.headlinesTableView.reloadData()
                
                
            })
            
        }else if(currentIndex==2){
                    NetworkTool.loadAllData(controllerTitle: "财经", completionHandler: { (response) in
                        self.economicsModelArr=response as! NSMutableArray
                        self.economicTableView.reloadData()
                        
                    })
            
        }else if(currentIndex==3){
                    NetworkTool.loadAllData(controllerTitle: "娱乐", completionHandler: { (response) in
                        self.recreationModelArr=response as! NSMutableArray
                        self.recreationTableView.reloadData()
                        
                    })

            
        }else if(currentIndex==4){
                    NetworkTool.loadAllData(controllerTitle: "军事", completionHandler: { (response) in
                        self.militaryModelArr=response as! NSMutableArray
                        self.militaryTableView.reloadData()
                       
                    })
            
        }else if(currentIndex==5){
                    NetworkTool.loadAllData(controllerTitle: "历史", completionHandler: { (response) in
                        self.historyModelArr=response as! NSMutableArray
                        self.historyTableView.reloadData()
                        
                    })
            
        }else if(currentIndex==6){
                    NetworkTool.loadAllData(controllerTitle: "科技", completionHandler: { (response) in
                        self.technologyModelArr=response as! NSMutableArray
                        self.technologyTableView.reloadData()
                        
                       
                    })
            
        }

    }
    
    
    
    
}











